package com.ylxl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylxl.common.constant.UserConstants;
import com.ylxl.common.exception.BaseException;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.common.uuid.SnowFlakeUtil;
import com.ylxl.entity.LaborProtection;
import com.ylxl.entity.User;
import com.ylxl.entity.reponse.LaborProtectionVO;
import com.ylxl.entity.request.*;
import com.ylxl.mapper.LaborProtectionMapper;
import com.ylxl.service.ILaborProtectionService;
import org.apache.commons.compress.utils.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户 业务层处理
 *
 */
@Service
public class LaborProtectionServiceImpl extends ServiceImpl<LaborProtectionMapper, LaborProtection>  implements ILaborProtectionService {
    private static final Logger log = LoggerFactory.getLogger(LaborProtectionServiceImpl.class);
    @Resource
    private LaborProtectionMapper laborProtectionMapper;
    @Resource
    private LaborProtectionSizeServiceImpl laborProtectionSizeService;

    @Override
    public List<LaborProtectionVO> selectLabelList(QueryLaborProtection queryLaborProtection) {
        List<LaborProtectionVO> laborProtectionVOList = Lists.newArrayList();
        LambdaQueryWrapper<LaborProtection> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(StringUtils.isNotEmpty(queryLaborProtection.getLaborName()),LaborProtection::getLaborName,queryLaborProtection.getLaborName())
                .eq(StringUtils.isNotEmpty(queryLaborProtection.getLaborCode()),LaborProtection::getLaborCode,queryLaborProtection.getLaborCode())
                .orderByDesc(LaborProtection::getCreateTime);
        List<LaborProtection> laborProtections = laborProtectionMapper.selectList(labelLambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(laborProtections)){
            laborProtections.forEach(l->{
                LaborProtectionVO laborProtectionVO = new LaborProtectionVO();
                BeanUtil.copyProperties(l,laborProtectionVO);//属性拷贝
                laborProtectionVO.setLaborProtectionSizeList(laborProtectionSizeService.selectMateriaList(new QueryLaborProtectionSize().setLaborId(l.getId())));
                laborProtectionVOList.add(laborProtectionVO);
            });
        }

        return laborProtectionVOList;
    }

    @Override
    public boolean checkNameUnique(LaborProtectionCmdOrMdy laborProtectionCmdOrMdy) {
        if(StringUtils.isEmpty(laborProtectionCmdOrMdy.getLaborName())){
            throw new BaseException("劳保名称不能为空");
        }
        LambdaQueryWrapper<LaborProtection> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(LaborProtection::getLaborName,laborProtectionCmdOrMdy.getLaborName())
                .ne(StringUtils.isNotEmpty(laborProtectionCmdOrMdy.getId()), LaborProtection::getId,laborProtectionCmdOrMdy.getId());
        List<LaborProtection> labels = laborProtectionMapper.selectList(labelLambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(labels)){
            return UserConstants.UNIQUE;
        }
        return UserConstants.NOT_UNIQUE;
    }

    @Override
    public int insertLabel(LaborProtectionCmdOrMdy laborProtectionCmdOrMdy) {
        LaborProtection laborProtectionInsert = new LaborProtection();
        BeanUtil.copyProperties(laborProtectionCmdOrMdy, laborProtectionInsert);//属性拷贝
        //生成随机字符串
        laborProtectionInsert.setLaborCode(SnowFlakeUtil.getRandomStr());
        int inseint = laborProtectionMapper.insert(laborProtectionInsert);
        List<MateriaCmd> materiaCmdList = laborProtectionCmdOrMdy.getMateriaCmdList();
        if(CollectionUtil.isNotEmpty(materiaCmdList)){
            //插入列表
            materiaCmdList.forEach(m->{
                laborProtectionSizeService.insertMateriar(new MateriaCmd()
                        .setMaterialCode(m.getMaterialCode())
                        .setSize(m.getSize())
                        .setLaborId(laborProtectionInsert.getId()));
            });
        }
        return inseint;
    }

    @Override
    public int updateLabel(LaborProtectionCmdOrMdy laborProtectionCmdOrMdy) {
        if(StringUtils.isEmpty(laborProtectionCmdOrMdy.getId())){
            throw new BaseException("劳保id不能为空");
        }
        LaborProtection laborProtectionUpdate = new LaborProtection();
        BeanUtil.copyProperties(laborProtectionCmdOrMdy, laborProtectionUpdate);//属性拷贝
        laborProtectionSizeService.remove(new MateriaRemove().setLaborId(laborProtectionCmdOrMdy.getId()));
        List<MateriaCmd> materiaCmdList = laborProtectionCmdOrMdy.getMateriaCmdList();
        if(CollectionUtil.isNotEmpty(materiaCmdList)){
            //插入列表
            materiaCmdList.forEach(m->{
                laborProtectionSizeService.insertMateriar(new MateriaCmd()
                        .setMaterialCode(m.getMaterialCode())
                        .setSize(m.getSize())
                        .setLaborId(laborProtectionCmdOrMdy.getId()));
            });
        }
        return laborProtectionMapper.updateById(laborProtectionUpdate);
    }

    @Override
    public LaborProtectionVO selctById(Integer id) {
        if(StringUtils.isEmpty(id)){
            throw new BaseException("劳保id不能为空");
        }
        LaborProtection laborProtection = laborProtectionMapper.selectById(id);
        LaborProtectionVO laborProtectionVO = new LaborProtectionVO();
        BeanUtil.copyProperties(laborProtection,laborProtectionVO);//属性拷贝
        laborProtectionVO.setLaborProtectionSizeList(laborProtectionSizeService.selectMateriaList(new QueryLaborProtectionSize().setLaborId(laborProtection.getId())));
        return laborProtectionVO;
    }

    @Override
    public int deleteById(Integer id) {
        if(StringUtils.isEmpty(id)){
            throw new BaseException("劳保id不能为空");
        }
        return laborProtectionMapper.deleteById(id);
    }

    @Override
    public int enableOrDisable(StatusRequest statusRequest) {
        if(StringUtils.isEmpty(statusRequest.getId())){
            throw new BaseException("id不能为空");
        }
        LaborProtection laborProtectionUpdate = new LaborProtection();
        BeanUtil.copyProperties(statusRequest, laborProtectionUpdate);//属性拷贝
        return laborProtectionMapper.updateById(laborProtectionUpdate);
    }
}
